﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Jason.Client.ComponentModel;
using Raven.Client;
using Topics.Radical.Validation;

namespace SampleApp.MvcBackend.Controllers
{
	public class HomeController : Controller
	{
		readonly IWorkerServiceClientFactory jasonFactory;
		readonly IDocumentStore sessionFactory;

		public HomeController( IWorkerServiceClientFactory jasonFactory, IDocumentStore sessionFactory )
		{
			Ensure.That( jasonFactory ).Named( () => jasonFactory ).IsNotNull();
			Ensure.That( sessionFactory ).Named( () => sessionFactory ).IsNotNull();

			this.jasonFactory = jasonFactory;
			this.sessionFactory = sessionFactory;
		}

		public ActionResult Index()
		{
			ViewBag.Message = "The list of customers, denormalized, defined in our amazing ASP.NET MVC application.";

			using ( var session = this.sessionFactory.OpenSession() )
			{
				var all = session.Query<Data.Views.CustomerView>().ToList();
				return View( all );
			}
		}

		public ActionResult CreateCustomer()
		{
			using ( var client = this.jasonFactory.CreateClient() )
			{
				client.Execute( new Commands.CreateNewCustomerCommand() { CustomerName = "sample customer" } );
			}

			return RedirectToAction( "Index" );
		}
	}
}
